package cn.gdsdxy.controller;

import cn.gdsdxy.pojo.Goods;
import cn.gdsdxy.service.impl.GoodsServicelmpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class GoodsController {
    @Autowired
    private GoodsServicelmpl goodsServicelmpl;
    @GetMapping("/getAllGoods")
    public String getAllGoods(Model model){
        List<Goods> goodsList=goodsServicelmpl.getAllGoods();
        model.addAttribute("goodsList",goodsList);
        return "goods";
    }
    @GetMapping("/getidGoods")
    public  String getidGoods(Model model,int id){
        List<Goods> goodsList=goodsServicelmpl.getidGoods(id);
        model.addAttribute("goodsList",goodsList);
        return "idgoods";
    }

    @RequestMapping(value = "/addGoods",method = RequestMethod.POST)
    @ResponseBody
    public String addGoods(int id,String name,String price){
        String msg;
        Goods goods=new Goods(id,name,price);
        if (goodsServicelmpl.addGoods(goods)>0)
            msg="sucess";
        else
            msg="fail";
        return msg;
    }

    @RequestMapping(value = "/deleteGoods",method = RequestMethod.POST)
    @ResponseBody
    public String deleteGoods(int id){
        String a;
        if (goodsServicelmpl.deleteGoods(id)>0)
            a="sucess";
        else
            a="fail";
        return a;
    }

    @RequestMapping(value = "/updataGoods",method = RequestMethod.POST)
    @ResponseBody
    public String updataGoods(int id, String name,String price){
        Goods goods= new Goods(id,name,price);
        String b;
        if (goodsServicelmpl.updataGoods(goods)>0)
            b="sucess";
        else
            b="fail";
        return b;
    }

    @GetMapping("/addGoodsPage")
    public String toAddGoodsPage(){
        return "addGoods";
    }
    @GetMapping("/deleteGoodsPage")
    public String todeleteGoods(){return "deleteGoods";}
    @GetMapping("/updataGoodsPage")
    public String toupdataGoods(){return "updataGoods";}
    @GetMapping("/getidGoodsPage")
    public String togetidGoods(){return "getidGoods";}
}
